<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

include 'secure/access_securities.php';
class Control_jobseekers extends Access_securities {

	public function __construct(){
		parent::__construct("control-jobseekers");
		$this -> form_validation -> set_error_delimiters('<p class="help-block danger">', '</p>');
	}
	public function index()
	{
		$data['title']="Manage Jobseekers";
		$data['jobseekers']=$this->model_globals->select_all('job_seekers')->result();
		$this->load->view(B_MASTER."index",$data);
	}
	public function insert_jobseeker(){
		$data['title']="Create Jobseekers";
		$module_id = str_replace('-', '_', $this -> uri -> segment(1));
		$this -> is_has_modify_permission($module_id);
		
		$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[job_seekers.email]');
		$this->form_validation->set_rules('first_name', 'First Name', 'required|min_length[2]|max_length[25]');
		$this->form_validation->set_rules('last_name', 'Last Name', 'required|min_length[2]|max_length[25]');
		$this -> form_validation -> set_rules('title', 'Title', 'required');		
		$this->form_validation->set_rules('password', 'Password', 'required|matches[conf_password]');
		$this->form_validation->set_rules('conf_password', 'Password Confirmation', 'required');
			
		if ($this -> form_validation -> run() === FALSE) {
			$this->load->view(B_MASTER."index",$data);
		} else {
			$token_key="";
			$picture="user.jpg";
			$data=array(
				'email'=>$this->input->post('email',TRUE),
				'password'=>sha1($this->input->post('password',TRUE)),
				'title'=>$this->input->post('title',TRUE),
				'first_name'=>$this->input->post('first_name',TRUE),
				'last_name'=>$this->input->post('last_name',TRUE),
				'address'=>$this->input->post('address',TRUE),
				'phone'=>$this->input->post('phone',TRUE),
				'mobile'=>$this->input->post('mobile',TRUE),
				'date_of_birth'=>$this->input->post('date_of_birth',TRUE),
				'gender'=>$this->input->post('gender',TRUE),
				'picture'=>$picture,
				'subscribe'=>'YES',
				'token_key'=>$token_key,
				'status'=>$this->input->post('active',TRUE),
				'created_date'=>null
			);
			
			$is_inserted=$this->model_globals->insert('job_seekers',$data);
			if($is_inserted){
				$this -> session -> set_flashdata('success', 'New job seeker  was created success !.');
				redirect('control-jobseekers');
			}else{
				$this -> session -> set_flashdata('error', 'New job seeker  was created fail !.');
				redirect('control-jobseekers');
			}
		}
	}
	public function update_jobseeker(){
		$data['title']="Update Jobseekers";
		$module_id = str_replace('-', '_', $this -> uri -> segment(1));
		$this -> is_has_modify_permission($module_id);
		
		$job_seeker_id=$this->uri->segment(3);
		
		//$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[job_seekers.email]');
		$this->form_validation->set_rules('first_name', 'First Name', 'required|min_length[2]|max_length[25]');
		$this->form_validation->set_rules('last_name', 'Last Name', 'required|min_length[2]|max_length[25]');
		$this -> form_validation -> set_rules('title', 'Title', 'required');		
		$this->form_validation->set_rules('password', 'Password', 'required|matches[conf_password]');
		$this->form_validation->set_rules('conf_password', 'Password Confirmation', 'required');
			
		if ($this -> form_validation -> run() === FALSE) {
			$data['job_seeker_info']=$this->model_globals->select_where('job_seekers',array('job_seeker_id'=>$job_seeker_id))->result();
			$this->load->view(B_MASTER."index",$data);
		} else {
			$token_key="";
			$data=array(
				'email'=>$this->input->post('email',TRUE),
				'password'=>sha1($this->input->post('password',TRUE)),
				'title'=>$this->input->post('title',TRUE),
				'first_name'=>$this->input->post('first_name',TRUE),
				'last_name'=>$this->input->post('last_name',TRUE),
				'address'=>$this->input->post('address',TRUE),
				'phone'=>$this->input->post('phone',TRUE),
				'mobile'=>$this->input->post('mobile',TRUE),
				'date_of_birth'=>$this->input->post('dob',TRUE),
				'gender'=>$this->input->post('gender',TRUE),
				'picture'=>$this->input->post('picture',TRUE),
				//'subscribe'=>$this->input->post('subscribe',TRUE),
				'status'=>$this->input->post('active',TRUE)
			);
			
			$is_updated=$this->model_globals->update('job_seekers',$data,array('job_seeker_id'=>$job_seeker_id));
			if($is_updated){
				$this -> session -> set_flashdata('success', 'Job seeker  information was updated success !.');
				redirect('control-jobseekers');
			}else{
				$this -> session -> set_flashdata('error', 'Job seeker information  was updated fail !.');
				redirect('control-jobseekers');
			}
		}
	}
	public function delete_jobseeker(){
		$job_seeker_id=$this->uri->segment(3);
		$is_deleted=$this->model_globals->delete('job_seekers',array('job_seeker_id'=>$job_seeker_id));
		if($is_deleted){
			$this -> session -> set_flashdata('success', 'Job seeker  information was deleted success !.');
			redirect('control-jobseekers');
		}else{
			$this -> session -> set_flashdata('error', 'Job seeker information  was deleted fail !.');
			redirect('control-jobseekers');
		}
	}
	public function disabled_jobseeker(){
		$job_seeker_id=$this->uri->segment(3);
		$data=array('status'=>0);
		$is_disabled=$this->model_globals->update('job_seekers',$data,array('job_seeker_id'=>$job_seeker_id));
		if($is_disabled){
			$this -> session -> set_flashdata('success', 'Job seeker  was disabled success !.');
			redirect('control-jobseekers');
		}else{
			$this -> session -> set_flashdata('error', 'Job seeker  was disabled fail !.');
			redirect('control-jobseekers');
		}
	}
	public function enabled_jobseeker(){
		$job_seeker_id=$this->uri->segment(3);
		$data=array('status'=>1);
		$is_enabled=$this->model_globals->update('job_seekers',$data,array('job_seeker_id'=>$job_seeker_id));
		if($is_enabled){
			$this -> session -> set_flashdata('success', 'Job seeker  was enabled success !.');
			redirect('control-jobseekers');
		}else{
			$this -> session -> set_flashdata('error', 'Job seeker  was enabled fail !.');
			redirect('control-jobseekers');
		}
	}
}
